package com.open.demo.mvi

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.launch

/**
 *
 * @Author: hailong.qiu 356752238@qq.com
 * @Maintainer: hailong.qiu 356752238@qq.com
 * @Date: 2022/6/10
 * @Copyright: 2022 www.andriodtvdev.com Inc. All rights reserved.
 */
class ImageViewModel : ViewModel() {

    val userIntent = Channel<ImageIntent>(Channel.UNLIMITED)

    private val _state = MutableStateFlow<ImageState>(ImageState.LoadingState)
    val state: StateFlow<ImageState>
        get() = _state

    fun handleIntent() {
        viewModelScope.launch {
            userIntent.consumeAsFlow().collect {
                if (it is ImageIntent.ImageData) {
                    // 判断是否可以设置图像模式
                    _state.value = ImageState.ErrorState("无法设置图像模式")
                    //
                    it.mode
                    // 调用API，设置图像模式
                }
            }
        }
    }

}